package com.example.hencoderdemo.lessonView;


import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.hencoderdemo.Common.Utils;

public class CaneraView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Camera camera = new Camera();
    int picWidth = 600;
    int rotateDegree = 0;
    int cameraDegree_up = 0;
    int cameraDegree_bottom = 0;
    int defaultTop;
    int defaultLeft;



    public CaneraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        // -8 = -8 * 72
        camera.setLocation(0, 0, Utils.getZForCamera());
    }

    public int getPicWidth() {
        return picWidth;
    }

    public void setPicWidth(int picWidth) {
        this.picWidth = picWidth;
    }

    public int getRotateDegree() {
        return rotateDegree;
    }

    public void setRotateDegree(int rotateDegree) {
        this.rotateDegree = rotateDegree;
        invalidate();
    }

    public int getDefaultTop() {
        return defaultTop;
    }

    public void setDefaultTop(int defaultTop) {
        this.defaultTop = defaultTop;
    }

    public int getDefaultLeft() {
        return defaultLeft;
    }

    public void setDefaultLeft(int defaultLeft) {
        this.defaultLeft = defaultLeft;
    }

    public int getCameraDegree_up() {
        return cameraDegree_up;
    }

    public void setCameraDegree_up(int cameraDegree_up) {
        this.cameraDegree_up = cameraDegree_up;
        invalidate();
    }

    public int getCameraDegree_bottom() {
        return cameraDegree_bottom;
    }

    public void setCameraDegree_bottom(int cameraDegree_bottom) {
        this.cameraDegree_bottom = cameraDegree_bottom;
        invalidate();
    }
    public void setCameraDegree_both(int cameraDegree_bottom) {
        this.cameraDegree_bottom = cameraDegree_bottom;
        this.cameraDegree_up = cameraDegree_bottom;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec,widthMeasureSpec);
    }
    boolean isClip = false;

    public boolean isClip() {
        return isClip;
    }

    public void setClip(boolean clip) {
        isClip = clip;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        defaultTop = getHeight()/2 - picWidth/2;
        defaultLeft = getWidth()/2 - picWidth/2;
        //绘制上半部分
        canvas.save();
        canvas.translate(defaultLeft+picWidth/2,defaultTop+picWidth/2);
        canvas.rotate(-rotateDegree);
        camera.save();
        camera.rotateX(cameraDegree_up);
        camera.applyToCanvas(canvas);
        camera.restore();
        canvas.clipRect(-picWidth,-picWidth,picWidth,0);
        canvas.rotate(rotateDegree);
        canvas.translate(-defaultLeft-+picWidth/2,-defaultTop-+picWidth/2);
        canvas.drawBitmap(Utils.getAvatar(getResources(),picWidth),defaultLeft,defaultTop,paint);
        canvas.restore();

        //绘制下半部分
        canvas.save();
        canvas.translate(defaultLeft+picWidth/2,defaultTop+picWidth/2);
        canvas.rotate(-rotateDegree);
        camera.save();
        camera.rotateX(cameraDegree_bottom);
        camera.applyToCanvas(canvas);
        camera.restore();
        canvas.clipRect(-picWidth,0,picWidth,picWidth);
        canvas.rotate(rotateDegree);
        canvas.translate(-defaultLeft-+picWidth/2,-defaultTop-+picWidth/2);
        canvas.drawBitmap(Utils.getAvatar(getResources(),picWidth),defaultLeft,defaultTop,paint);
        canvas.restore();

        //清空
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//        canvas.drawPaint(paint);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        if (isClip){
            canvas.drawColor(Color.parseColor("#FFC0CB"));
            int pad = 100;
            int padHeight = 140;
            Path path = new Path();
            //定义路径的起始位置，每次都要重新定义
            path.moveTo(getWidth()/2 - pad,getHeight()/2 - picWidth/2 + padHeight);
            path.cubicTo(getWidth()/2 - pad,getWidth()/2 - picWidth/2+ padHeight,
                    getWidth()/2+picWidth/2-picWidth/4 + pad/2,getWidth()/2-picWidth/2 -picWidth/2,
                    getWidth()/2+picWidth/2,getWidth()/2-picWidth/2+ padHeight);

            path.moveTo(getWidth()/2 - picWidth/2,getHeight()/2 - picWidth/2 + padHeight);
            path.cubicTo(getWidth()/2- picWidth/2,getWidth()/2 - picWidth/2+ padHeight ,
                    getWidth()/2-picWidth/2+picWidth/4 - pad/2,getWidth()/2-picWidth/2 -picWidth/2,
                    getWidth()/2 +pad,getWidth()/2-picWidth/2+ padHeight);

            path.moveTo(getWidth()/2 - picWidth/2,getHeight()/2 - picWidth/2+ padHeight);
            path.cubicTo(getWidth()/2 - picWidth/2,getHeight()/2 - picWidth/2+ padHeight,
                    getWidth()/2 - picWidth/2,getHeight()/2+picWidth/3,
                    getWidth()/2,getHeight()/2+picWidth/2);

            path.moveTo(getWidth()/2+picWidth/2,getHeight()/2 - picWidth/2+ padHeight);
            path.cubicTo(getWidth()/2 + picWidth/2,getHeight()/2 - picWidth/2+ padHeight,
                    getWidth()/2 + picWidth/2,getHeight()/2+picWidth/3,
                    getWidth()/2,getHeight()/2+picWidth/2);

            //三角形

            path.moveTo(getWidth()/2 - picWidth/2,getHeight()/2 - picWidth/2+ padHeight);
            path.lineTo(getWidth()/2+picWidth/2,getHeight()/2 - picWidth/2+ padHeight);
            path.lineTo(getWidth()/2,getHeight()/2 + picWidth/2);
            path.close();

            canvas.clipPath(path);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(Utils.getAvatar(getResources(),picWidth),defaultLeft,defaultTop,paint);
        }




    }
}
